﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using HospitalSystem.Core;
using System.Drawing.Drawing2D;
namespace HospitalSystem
{
    public partial class rightsinfo : System.Web.UI.Page
    {
        string strConnString = ConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
        private show_Image _showimage;
        private regfamilyLog _regf;
        private RegisterLog _RL;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CID"] == null)
            {

                MessageBoxurl("กรุณากดค้นหา หรือลงทะเบียนเด็กก่อน", "findchild.aspx");

            }
            else
            {

                _showimage = new show_Image();
                _regf = new regfamilyLog();
                _RL = new RegisterLog();

                showsession();
                _showimage.show_Images(cid_label, Image1);




                if (!IsPostBack)
                {

                    _RL.radiomedicalcare(medicalcare);

                    _RL.radioyesno_1(yesno1);
                    _RL.radioyesno_2(yesno2);

                    _RL.loaddatarightsinfo(cid_label, MedicalIDcard, Dateofissue, Dateofexp, PrimaryCareClinic, CareClinicForward, NumberDisabilitieID, NumberDisabilitieIDdate, NumberDisabilitieIDexp);

                    _RL.loaddatadropdownDisabilityType(DisabilityType);
                    _RL.loaddataradiomedicalcare(cid_label, medicalcare);

                    _RL.loaddataradioDisabilityAllowance(cid_label, yesno1);
                    _RL.loaddataradioCouponStudy(cid_label, yesno2);
                    _RL.loaddataDisabilityType(cid_label, DisabilityType);
                }
            }
            
           
        }








   

        private void MessageBoxurl(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }
        protected void showsession()
        {
            String ShowValue = (string)Session["CID"];
            cid_label.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            lname.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            fname.Text = ShowValue3;



        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            ImageUpload_note();
            ImageUpload_medicalcare();
            _RL.insertregdatarightsinfo(cid_label, MedicalIDcard.Text, Dateofissue.Text, Dateofexp.Text, PrimaryCareClinic.Text, CareClinicForward.Text, NumberDisabilitieID.Text, NumberDisabilitieIDdate.Text, NumberDisabilitieIDexp.Text, medicalcare,yesno1,yesno2,DisabilityType);

        //    Response.Redirect("~/Information.aspx");
        }

       





        //หมายเลขบัตรคนพิการ
        protected void ImageUpload_note()
        {
            SqlConnection conn = new SqlConnection(strConnString);
            if (FileUploader.HasFile)
            {


                conn.Open();
                SqlCommand cmd = new SqlCommand("delete from pic_note where cid=@CID", conn);
                cmd.Parameters.AddWithValue("@CID", cid_label.Text);
                cmd.ExecuteNonQuery();

                conn.Close();


                string MapPath = ("~/pic/note/");
                string filename = cid_label.Text;
                FileUploader.SaveAs(Server.MapPath(MapPath) + filename + Path.GetExtension(FileUploader.FileName));


                string Includepath = ((MapPath) + filename + Path.GetExtension(FileUploader.FileName));
                Bitmap originalBMP = new Bitmap(FileUploader.FileContent);
                // ใส่ขนาดที่นี่
                int newWidth = 640;
                int newHeight = 480;
                Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                Graphics oGraphics = Graphics.FromImage(newBMP);
                oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
                newBMP.Save(Server.MapPath(MapPath + filename + Path.GetExtension(FileUploader.FileName)));
                originalBMP.Dispose();
                newBMP.Dispose();
                oGraphics.Dispose();





                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection scon = new SqlConnection(constr);
                SqlCommand cmd22 = new SqlCommand("INSERT INTO [pic_note] ([cid],[FilePath],dateupdate) VALUES (@Iddek,@Filepath,@dateupdate)", scon);
                cmd22.Parameters.AddWithValue("@Iddek", cid_label.Text);
                cmd22.Parameters.AddWithValue("@FilePath", Includepath);
                cmd22.Parameters.AddWithValue("@dateupdate", DateTime.Now);


                scon.Open();
                cmd22.ExecuteNonQuery();
                scon.Close();


            }



        }






             protected void ImageUpload_medicalcare()
        {
            SqlConnection conn = new SqlConnection(strConnString);
            if (FileUploader_medicalcare.HasFile)
            {
             
              
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("delete from pic_medicalcare where cid=@CID", conn);
                    cmd.Parameters.AddWithValue("@CID", cid_label.Text);
                    cmd.ExecuteNonQuery();

                    conn.Close();


                    string MapPath = ("~/pic/medicalcare/");
                    string filename = cid_label.Text;
                    FileUploader_medicalcare.SaveAs(Server.MapPath(MapPath) + filename + Path.GetExtension(FileUploader_medicalcare.FileName));
               

                    string Includepath = ((MapPath) + filename + Path.GetExtension(FileUploader_medicalcare.FileName));
                    Bitmap originalBMP = new Bitmap(FileUploader_medicalcare.FileContent);
                    // ใส่ขนาดที่นี่
                    int newWidth = 640;
                    int newHeight = 480;
                    Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                    Graphics oGraphics = Graphics.FromImage(newBMP);
                    oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
                    newBMP.Save(Server.MapPath(MapPath + filename + Path.GetExtension(FileUploader_medicalcare.FileName)));
                    originalBMP.Dispose();
                    newBMP.Dispose();
                    oGraphics.Dispose();





                    string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                    SqlConnection scon = new SqlConnection(constr);
                    SqlCommand cmd22 = new SqlCommand("INSERT INTO [pic_medicalcare] ([cid],[FilePath],dateupdate) VALUES (@Iddek,@Filepath,@dateupdate)", scon);
                    cmd22.Parameters.AddWithValue("@Iddek", cid_label.Text);
                    cmd22.Parameters.AddWithValue("@FilePath", Includepath);
                    cmd22.Parameters.AddWithValue("@dateupdate", DateTime.Now);


                    scon.Open();
                    cmd22.ExecuteNonQuery();
                    scon.Close();

           
            }
















        }
    }
}